package com.yf.dxs.wo.service.impl;

import com.alibaba.fastjson2.JSON;
import com.yf.dxs.wo.consts.WoEventConst;
import com.yf.dxs.wo.dao.WoCardMsgMapper;
import com.yf.dxs.wo.entity.WoCardMsgDO;
import com.yf.dxs.wo.redis.RedisMqTemplate;
import com.yf.dxs.wo.req.msg.WoCardMsg;
import com.yf.dxs.wo.service.MsgHandler;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * https://doc.weimobcloud.com/detail?menuId=19&childMenuId=1&tag=2483&id=2994&isold=2
 * 文档字段不一定准 以实际为准
 * 注册时候会先通知
 * 成为客户
 * 成为会员
 * 开卡
 */
@Service
@Slf4j
public class MemberCardHandler implements MsgHandler {

    @Resource
    private WoCardMsgMapper woCardMapper;

    @Resource
    private RedisMqTemplate redisMqTemplate;



    public void handle(String msgId, String msg, String event) {
        //开卡事件
        if (event.equals(WoEventConst.CREATE)) {
            WoCardMsg woCardMsg = JSON.parseObject(msg, WoCardMsg.class);
            WoCardMsgDO woCard = woCardMsg.toEntity();
            woCard.setMsgId(msgId);
            log.info("处理开卡:{},{},{},{}", woCard.getWid(), woCard.getCardNo(), woCard.getCustomCardNo(), woCard.getCreateTime());
            int i = woCardMapper.insert(woCard);
            if (i == 0) {
                log.error("插入失败:{},{},{},{}", woCard.getWid(), woCard.getCardNo(), woCard.getCustomCardNo());
            } else {
                log.info("插入成功:{},{},{},{}", woCard.getWid(), woCard.getCardNo(), woCard.getCustomCardNo());
                redisMqTemplate.sendMsg(woCard);
            }
        }
    }
}
